﻿using UnityEngine;



[RequireComponent(typeof(MovementComponent))]
public class FirstPersonTurnComponent : MonoBehaviour
{
    [Header("Look Settings")]
    [SerializeField] private float mouseSensitivity = 100f;
    [SerializeField] private Transform cameraPivot;
    [SerializeField] private float verticalClamp = 85f;

    private MovementComponent _movement;
    private float _xRotation;

    void Awake()
    {
        _movement = GetComponent<MovementComponent>();
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        // 获取鼠标输入
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        // 水平旋转（角色身体）
        transform.Rotate(Vector3.up * mouseX);

        // 垂直旋转（摄像机）
        _xRotation -= mouseY;
        _xRotation = Mathf.Clamp(_xRotation, -verticalClamp, verticalClamp);
        cameraPivot.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);

        // 同步移动方向到视角前方
        _movement.MoveInWorldSpaceAlongCamera(cameraPivot.forward);
    }
}